mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});
var order = new Vue({
	el: '#order',
	data: {
		imgCommonUrl: common.imageUrl,
		orderCheck: 0,
		ordernav: [{
				text: '全部',
				val: ''
			}, {
				text: '待付款',
				val: 1
			}, {
				text: '待收货',
				val: 3
			}, {
				text: '已完成',
				val: 4
			},
			{
				text: '已取消',
				val: 5
			}

		],
		pageNum: 1,
		list: []

	},
	methods: {
		//选项卡切换
		ordernavCheck: function(index) {
			if(this.orderCheck == index) {
				return
			}
			this.orderCheck = index
			this.pageNum = 1
			this.list = []
			mui('#pullrefresh').pullRefresh().refresh(true);
			mui('#pullrefresh').pullRefresh().pullupLoading();
			mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 0.1);
		},
		//去付款
		payment: function(data) {
			console.log(data.subId)
			common.open('../order/orderdetails.html', 'orderdetails.html', data.subId)
		},
		//订单详情
		openOrderDetail: function(data) {
			console.log(data.subNum)
			common.open('../order/ordersubmit.html', '../order/ordersubmit.html', data.subNum)
		},
		//申请退货
		returnGoods: function(list) {
			var temp = {
				parent: list
			}
			common.open('../order/returngoods.html', '../order/returngoods.html', temp)
		},
		//确定收获
		collectGoods: function(list, index) {
			var btnArray = ['取消', '确定'];
			mui.confirm('是否确定收货', '', btnArray, function(e) {
				if(e.index == 1) {
					common.ajax('/userOrder/orderReceive/' + list.subNum, {}, function(data) {
						if(data.code == 1) {
							if(order.ordernav[order.orderCheck].val != '') {
								order.list.splice(index, 1)
							} else {
								order.pageNum = 1
								order.list = []
								mui('#pullrefresh').pullRefresh().refresh(true);
								mui('#pullrefresh').pullRefresh().pullupLoading();
								mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 0.1);
							}
						} else {
							if(data.resultMsg) {
								common.toast(data.resultMsg)
							}
						}
					})
				}
			}, 'div')

		},
		goComment: function() {
			common.open('../comment/myComment.html', '../comment/myComment.html')
		},
		opDetails: function(data) {
			addShop(0, data)
			function addShop(check, data) {
				if(data.subOrderItemDtos[check]) {
					var skuId = data.subOrderItemDtos[check].skuId + ':' + data.subOrderItemDtos[check].basketCount + ','
					var dataAjax = {
						prodId: data.subOrderItemDtos[check++].prodId,
						skuId: skuId.substring(0, skuId.length - 1)
					}
					common.ajax('/shopCart/addShopBuy',dataAjax, function(dataMain) {
						addShop(check, data)
					})
				} else {
					if(plus.webview.getWebviewById('shopping.html')) {
						plus.webview.getWebviewById('shopping.html').reload()
					}
					var list = plus.webview.getWebviewById("ylindex.html")
					mui.fire(list, 'shopping');
					mui.back()
				}
			}

			//			common.open('../details/details.html', '../details/details.html', data.prodId)
		}
	}

})
document.addEventListener('order', function() {
	order.pageNum = 1
	order.list = []
	mui('#pullrefresh').pullRefresh().refresh(true);
	mui('#pullrefresh').pullRefresh().pullupLoading();
	mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 0.1);
})

mui.plusReady(function() {
	var self = plus.webview.currentWebview();
	if(self.parameter) {
		order.orderCheck = self.parameter
	}
	if(mui.os.plus) {
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().pullupLoading();
		}, 10);
	} else {
		mui.ready(function() {
			mui('#pullrefresh').pullRefresh().pullupLoading();
		});
	}

})

function pullupRefresh() {
	common.ajax('/userOrder/getData', {
		curPageNO: order.pageNum++,
		state_type: order.ordernav[order.orderCheck].val
	}, function(data) {
		if(data.code == 1) {
			if(data.data.pageCount < order.pageNum) {
				mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
			} else {
				mui('#pullrefresh').pullRefresh().endPullupToRefresh()
			}
			order.list = order.list.concat(data.data.list)

			order.$nextTick(function() {
				mui('.muiScrollx').scroll({
					deceleration: 0.0005, //flick 减速系数，
					scrollY: false,
					scrollX: true,
					indicators: false
				});

			})
		} else {
			mui('#pullrefresh').pullRefresh().endPullupToRefresh()
			if(data.resultMsg) {
				common.toast(data.resultMsg)
			}
		}
	}, true, 'get')
}